package td3.view;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import td3.model.Contact;
import td3.model.ContactNodeJPanelTransferHandler;
import td3.model.ContactNodeTreeTransferHandler;

public class SuppView extends JFrame {

	private JPanel panel;

	private JTextField name;
	private JTextField email;
	private JLabel nameLabel;
	private JLabel emailLabel;
	private JLabel img;
	
	public SuppView() throws HeadlessException {
		super();
		this.setSize(200, 200);
		this.setTitle("Drag&Drop");
		this.panel = new JPanel();
		this.add(panel);
		this.panel.setSize(200, 200);
		this
				.setTransferHandler(new ContactNodeJPanelTransferHandler(this));
		setVisible(true);

		name = new JTextField();
		email = new JTextField();

		nameLabel = new JLabel();
		nameLabel.setText("Nom : ");
		nameLabel.setLabelFor(name);
		emailLabel = new JLabel();
		emailLabel.setText("Email : ");
		emailLabel.setLabelFor(email);
		img = new JLabel();
		this.add(img);
		setLayout(new GridBagLayout());

		JLabel[] labels = { nameLabel, emailLabel };
		JTextField[] textFields = { name, email };
		addLabelTextRows(labels, textFields, this);
	}

	public void setContact(Contact contact) {
		name.setText(contact.getNom());
		email.setText(contact.getMail());
		img.setIcon(new ImageIcon(contact.getIcone()));
	}

	private void addLabelTextRows(JLabel[] labels, JTextField[] textFields,
			Container container) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		int numLabels = labels.length;

		for (int i = 0; i < numLabels; i++) {
			c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last
			c.fill = GridBagConstraints.NONE; // reset to default
			c.weightx = 0.0; // reset to default
			container.add(labels[i], c);

			c.gridwidth = GridBagConstraints.REMAINDER; // end row
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 1.0;
			container.add(textFields[i], c);
		}
	}
}
